package com.mantou.proxyservice.scheduler;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.mantou.cdl.net.HttpInvoker;
import com.mantou.proxyservice.proxeservice.entity.Proxy;
import com.mantou.proxyservice.proxeservice.entity.Testurl;
import com.mantou.proxyservice.proxeservice.service.ProxyService;
import com.mantou.proxyservice.proxeservice.service.TesturlService;
import com.mantou.proxyservice.utils.ProxyStatus;
import com.mantou.proxyservice.utils.ProxyType;

public class Validater implements Runnable {

	@Autowired
	private ProxyService proxyService;
	
	@Autowired
	private TesturlService testurlService;
	
	private ThreadPoolTaskExecutor executor;
	
	private int deletesize = 20;
	
	public ThreadPoolTaskExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(ThreadPoolTaskExecutor executor) {
		this.executor = executor;
	}

	public void run() {
		// TODO Auto-generated method stub
		List<Proxy> needupdate = proxyService.find4update();
		//needupdate.clear();
		if(needupdate.size() ==0)
			return;
		final List<Testurl> testurls = testurlService.findall();
		if(testurls.size() == 0){
			return;
		}
		
		for(Proxy proxy:needupdate){
			executor.execute(new ProxyTester(proxy,testurls));
		}
	}

	private class ProxyTester implements Runnable{
		private Proxy proxy;
		private List<Testurl> testurls;
		
		public ProxyTester(Proxy proxy, List<Testurl> testurls) {
			super();
			this.proxy = proxy;
			this.testurls = testurls;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//now ignore socket and vpn
			if(proxy.getType()!= null && (proxy.getType()==ProxyType.SOCKET.getType() ||proxy.getType() == ProxyType.VPN.getType()))
					return;
			int passnumber =0;
			for(Testurl testurl:testurls){
				try {
					int httpcode = new HttpInvoker(testurl.getUrl()).setproxy(proxy.getIp(), proxy.getPort()).request().getStatusCode();
					if(httpcode == 200)
						passnumber ++;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			proxy.setStability(passnumber* 100 /testurls.size() );
			
			if(passnumber == testurls.size()){
				proxy.setStatus(ProxyStatus.ENABLE.getStatus());
				proxy.setTesttimes(0);
			}else if(passnumber <=0){
				Integer oldTestTimes = proxy.getTesttimes();
				proxy.setTesttimes(oldTestTimes == null?0:oldTestTimes+1);
				proxy.setStatus(ProxyStatus.DISABLE.getStatus());
			}else if(passnumber > testurls.size() * 0.9){
				proxy.setTesttimes(0);
				proxy.setStatus(ProxyStatus.ENABLE.getStatus());
			}else{
				proxy.setTesttimes(0);
				proxy.setStatus(ProxyStatus.HALFENBLE.getStatus());
			}
			proxy.setLastupdate(new Date());
			if(proxy.getTesttimes() >= deletesize){
				proxyService.deleteByPrimaryKey(proxy.getId());
			}else{
				proxyService.updateByPrimaryKey(proxy);
			}
		}
	}
}
